package com.itheima.controller;

import com.itheima.entity.ResultInfo;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public ResultInfo login(@RequestBody User user){
        User login = userService.login(user.getUsername(), user.getPassword());
        if (login != null){
            session.setAttribute("user",login);
            return new ResultInfo(true,"登录成功");
        }else {
            return new ResultInfo(false,"登陆失败");
        }
    }

    @RequestMapping("logout")
    public void logout(){
        session.invalidate();
    }
}
